<template>
  <div>
    <el-form :model="innerFrom" :rules="rules" ref="formRef">
      <el-form-item prop="name" style="margin-bottom: 20px;" label="收件人">
          <el-input v-model="innerFrom.name" placeholder="请输入收件人姓名"></el-input>
       </el-form-item>
      <el-form-item prop="phone" style="margin-bottom: 20px;" label="联系方式">
        <el-input v-model="innerFrom.phone" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item prop="address" style="margin-bottom: 20px;" label="收货地址">
        <el-input v-model="innerFrom.address" placeholder="请输入收货地址"></el-input>
      </el-form-item>
      <el-button plain style="margin-left: 300px; width: 44px;" type="primary" @click="addAddress">添加</el-button>
    </el-form>
  </div>  
</template>

<script setup>
import { get,post } from "@/plugins/axios"
import { ref,reactive } from "vue";
import {Path} from "@/constant/index"
const emits = defineEmits(['getAddress','update:formshow'])
// 添加地址表单
const innerFrom = reactive({
  name: '',
  address:'',
  phone:''
})
const rules = {  
  name: [  
    { required: true, message: '收件人不能为空', trigger: 'blur' }  
  ],  
  phone: [  
    { required: true, message: '联系方式不能为空', trigger: 'blur' },  
    {  
      pattern: /^1[3-9]\d{9}$/,  
      message: '请输入正确的手机号',  
      trigger: 'blur'  
    }  
  ],  
  address: [  
    { required: true, message: '收货地址不能为空', trigger: 'blur' }  
  ]  
};  
const formRef = ref(null);
// 后续要去掉OpenId这个参数
function addAddress(){
  formRef.value.validate((valid) => {  
    if (valid) {  
      post(Path.addressAdd,{
        name:innerFrom.name,
        address:innerFrom.address,
        phone:innerFrom.phone,
        },content => {
          // 提交成功再次刷新地址
          emits('getAddress')
          emits('update:formshow',false)
        })  
    } else {  
      console.log('表单验证失败');  
      return false;  
    }  
  });  
}
</script>

<style lang="scss" scoped>

</style>